package com.dhecp.project.business.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.business.domain.ProductGNumber;
import com.dhecp.project.business.mapper.ProductGNumberMapper;
import com.dhecp.project.business.service.ProductGNumberService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 型号条码记录 服务层
 * @author DH-Xxj
 * @version 2021年3月30日
 */
@Service
public class ProductGNumberServiceImpl implements ProductGNumberService {

	@Autowired
	private ProductGNumberMapper productGNumberMapper;

	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;

	/**
     * 查询型号条码信息
     * @param params
     * @return List - ProductGNumber
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
	public List<ProductGNumber> queryProductGNumberList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句

		if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "createTime desc, updateTime asc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();	
		}
		
		return productGNumberMapper.findProductGNumber(params);
	}

	/**
	 * 通过guid查询单条记录
	 * @param guid 必要参数：条码行唯一值
	 * @return 实体 ProductGNumber
	 * @author DH-Xxj
	 */
	@Override
	public ProductGNumber queryProductGNumberByGuid(String guid) {
		return productGNumberMapper.findProductGNumberByGuid(guid);
	}
	
	/**
	 * 新增型号条码信息
	 * @param gNumberItems
	 * @return AjaxResult
	 */
	@Override
	public AjaxResult insertProductGNumberInfo(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<ProductGNumber> productGNumberList = JSON.parseArray(params.get("gNumberItems").toString(), ProductGNumber.class);
		
		if (productGNumberList == null || productGNumberList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (ProductGNumber productGNumber : productGNumberList) {
			productGNumber.setCreateUser(ShiroUtils.getUserId());
			productGNumber.setCreateUserName(ShiroUtils.getUserName());
			productGNumber.setCreateTime(new Date());
			productGNumber.setUpdateUser(ShiroUtils.getUserId());
			productGNumber.setUpdateUserName(ShiroUtils.getUserName());
			productGNumber.setUpdateTime(new Date());
			productGNumberMapper.saveProductGNumber(productGNumber);
		}
		return AjaxResult.success("操作成功");
	}
	
}
